import 'package:enterprise_v8/app/core/core.dart';
import 'package:enterprise_v8/app/core/ext/widget_extension.dart';
import 'package:enterprise_v8/app/core/utils/log_util.dart';

extension AssetsX on String {
  Widget asImg({
    /// 点击图片是否显示大图
    bool clickShowLarge = false,

    /// 图片加载失败时的处理
    Widget Function()? handleErr,
    double? height,
    double? width,

    /// 图片的适应模式，仅对非svg起作用
    BoxFit? fit,

    /// 圆角
    double? radius,
  }) {
    try {
      var img = () {
        if (endsWith('.svg')) return SvgPicture.asset(this, height: height, width: width);
        if (endsWith('.png') || endsWith('.jpg') || endsWith('.jpeg') || endsWith('.gif') || endsWith('.webp')) {
          if (startsWith('http')) return Image.network(this, height: height, width: width, fit: fit);
          return Image.asset(this, height: height, width: width, fit: fit);
        }
      }();
      if (radius != null && img != null) img = ClipRRect(borderRadius: BorderRadius.circular(radius), child: img);
      if (img != null) return clickShowLarge ? img.clickShowLarge(url: this) : img;
      throw Exception('不支持的图片类型: $this');
    } catch (e) {
      AppLogger.devLog('图片加载失败: $this, $e');
      if (handleErr != null) return handleErr();
    }
    return AppGaps.zero;
  }
}
